package de.sendorian.app.forumArchive.matcher;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.joda.time.DateTime;
import org.joda.time.Interval;

import de.sendorian.app.forumArchive.domain.Topic;


/**
 * Matches Posts that have a date between a given start and end date (inclusive).
 * The time will not be checked, so the period is midnight if the start date until
 * end of the end date.
 * 
 * @author sendorian
 * @since 1.0 beta3
 * 
 */
public class TopicDateRangeMatcher extends TypeSafeDiagnosingMatcher<Topic> {

    private Topic topic;
    private DateTime start;
    private DateTime end;

    public TopicDateRangeMatcher(final DateTime start, final DateTime end) {
        this.start = start.withTime(0, 0, 0, 0);
        if (end == null) {
            this.end = new DateTime();
        } else {
            // set to midnight the next day
            this.end = end.plusDays(1).withTime(0, 0, 0, 0);
        }
    }


    @Override
    protected boolean matchesSafely(Topic item, Description mismatchDescription) {
        topic = item;

        Interval interval = new Interval(start, end);
        DateTime lastChangeDate = new DateTime(topic.getLastPost());
        if (interval.contains(lastChangeDate))
            return true;
        return false;
    }


    public void describeTo(Description description) {
        description.appendText(" that topic ");
        description.appendValue(topic);
        description.appendText(" is last post between ");
        description.appendValue(start);
        description.appendText(" and ");
        description.appendValue(end);
    }

}
